define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'room/index' + location.search,
                    add_url: 'room/add',
                    edit_url: 'room/edit',
                   // del_url: 'room/del',
                    multi_url: 'room/multi',
                    import_url: 'room/import',
                    table: 'user_details',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        //{field: 'user_id', title: '姓名', operate: false},
                        {field: 'prole', title:'上级', operate: false},
                        {field: 'img', title: __('Img'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'phone', title: __('Phone'), operate: 'LIKE'},
                        {field: 'age', title: __('Age'), operate: false},
                        {field: 'login_status', title: "状态",
                            searchList: {"1":"正常","0":"停用"},
                            formatter: Table.api.formatter.toggle,
                            search:false
                        },
                        {field: 'edu', title: __('Edu'), operate: false},
                        {field: 'exp_years', title: __('Exp_years'), operate: false},

                        {field: 'addr', title: __('Addr'), operate: false,table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                     {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'riderAssign',
                                    text: '分配上级',
                                    title: '分配上级',
                                    classname: 'btn btn-xs btn-info btn-click',
                                    icon: 'fa fa-flag',
                                    visible:function(row){
                                        if(row.user_id != null){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    click: function (e, row) {
                                        console.log('riderAssign', e, row);
                                        Controller.api.showRiderDialog(row.user_id);
                                    }
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
            $(document).ready(function () {
                const attachmentInput = $('#c-attachment');
                const downloadButton = $('#downloadAttachment');

                // 监听附件栏的变化
                attachmentInput.on('input', function () {
                    const attachmentValue = attachmentInput.val();

                    // 如果有附件，则显示下载按钮
                    if (attachmentValue) {
                        downloadButton.show();
                    } else {
                        downloadButton.hide();
                    }
                });

                // 下载按钮点击事件
                downloadButton.on('click', function () {
                    const attachmentUrls = attachmentInput.val().split(',').map(url => url.trim()); // 将附件链接分割并去除空格
                    let validUrlFound = false; // 用于检查是否有有效的 URL

                    // 创建一个链接元素并逐个下载
                    attachmentUrls.forEach(url => {
                        if (url) { // 检查是否为有效的 URL
                            validUrlFound = true;
                            const link = document.createElement('a');
                            link.href = url; // 设置下载链接
                            link.target = '_blank'; // 在新标签页中打开
                            link.download = ''; // 触发下载

                            // 触发下载
                            document.body.appendChild(link);
                            link.click();
                            document.body.removeChild(link);
                        }
                    });

                    if (!validUrlFound) {
                        alert('没有有效的附件链接！');
                    }
                });
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            // 骑手列表弹窗
            showRiderDialog: function (id) {
                Fast.api.open('agent/online', '列表1', {
                    callback: function (data) {
                        console.log('showRiderDialog callback', data);
                        Controller.api.riderAssign(id,data.row.user_id);
                    }
                });
            },
            // 指派骑手
            riderAssign: function (id, pid) {
                var url = 'companion/fenpei';
                var data = {
                    id: id,
                    pid: pid
                };
                Fast.api.ajax({
                    url: url,
                    data: data,
                    loading: true,
                    success: function (result) {
                        Layer.closeAll();
                        if(result.code==1){
                            Toastr.success(result.msg);
                        }else{
                            Toastr.error(result.msg);
                        }

                        $('#table').bootstrapTable('refresh');
                    }
                });
            },
        }
    };
    return Controller;
});
